﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebShopData;

namespace Projekt_2_ASP.NET
{
    public partial class About : Page
    {
        private Order _order;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "Beställ";
            ButtonSubmit.Visible = true;
            ButtonContinue.Visible = false;

            _order = (Order)Session["Order"];


            //List<OrderRow> lista = new List<OrderRow>();
            //lista.Add(new OrderRow(1, 1));
            //lista.Add(new OrderRow(2, 1));
            //_order = new Order(lista, "fsij", "hfadns", 64973, 1);
            var orderRows = _order.OrderRows;

            int[] identifiers = _order.OrderRows.Select(x => x.ArticleID).ToArray();
            Article[] articles = WebShopDal.ReadArticles(identifiers);
            List<string> names = articles.Select(x => x.Name).ToList();

            List<dynamic> list = new List<dynamic>();
            for (int i = 0; i < orderRows.Count; i++)
            {
                list.Add(new { Text = names[i], Quantity = orderRows[i].Quantity });
            }

            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            if ((Session["Order"] as Order).OrderRows.Count > 0)
            {
                (Session["Order"] as Order).UserID = int.Parse(Session["UserID"].ToString());
                (Session["Order"] as Order).ZipCode = int.Parse(TextBoxZipCode.Text.Replace(" ", ""));
                (Session["Order"] as Order).City = TextBoxCity.Text;
                (Session["Order"] as Order).Address = TextBoxAddress.Text;

                var blabla = (Session["Order"] as Order);

                WebShopDal.AddOrderToDb((Session["Order"] as Order));
                (Session["Order"] as Order).OrderRows = new List<OrderRow>();
                Label1.Visible = true;
                ButtonContinue.Visible = true;
                ButtonSubmit.Visible = false;
            }
            else
            {
                ButtonSubmit.Visible = true;
                ButtonContinue.Visible = false;
                Label1.Visible = true;
                Label1.ForeColor = Color.Red;
                Label1.Text = "Kundvagnen är tom!";
            }
        }

        protected void ButtonContinue_Click(object sender, EventArgs e)
        {
            Response.Redirect("Main.aspx");
        }
    }
}